package nl.haemers.testrunners
{
	import de.polygonal.ds.DListIterator;
	import de.polygonal.ds.DListNode;
	import de.polygonal.ds.LinkedListNode;
	import flash.display.Sprite;
	import flash.events.Event;
	import nl.haemers.base.BaseMovieClip;
	import nl.haemers.collections.AbstractCollection;
	import nl.haemers.collections.LinkedList;
	import nl.haemers.collections.LinkedNode;
	import nl.haemers.collections.ListIterator;
	import nl.haemers.test.data.vo.TestVO;
	import nl.haemers.utils.Logger;
	import nl.haemers.timer.StopWatch;
	
	import flash.utils.getTimer;
	
	import de.polygonal.ds.DLinkedList;
	//import nl.dpdk.collections.lists.LinkedList;
	/**
	 * @author Jonathan Haemers
	  */
	public class CollectionRunner extends BaseMovieClip
	{
		//public var list				:LinkedList;
		//public var linkedNode		:LinkedNode;
		public var vector			:Vector.<TestVO>;
		public var linkedList		:LinkedList;
		public var array			:Array;
		public var timer			:StopWatch;
		
		public var dLinked			:DLinkedList;
		//public var dpdkList			:nl.dpdk.collections.lists.LinkedList;
		
		public function CollectionRunner() 
		{
			super();
		}
		
		/** Public methods **/
		
		/** Getters & Setters **/
		
		/** Protected methods **/
		override protected function init(e:Event = null) : void
		{
			super.init();
			start();
		}
		
		public function start() : void
		{
			//createList();
			timer = new StopWatch();
			createCollection();
			//configListeners();
		}
		
		protected function createCollection() : void
		{
			linkedList = new LinkedList();
			vector = new Vector.<TestVO>();
			array = new Array();
			dLinked = new DLinkedList();
			//dpdkList = new nl.dpdk.collections.lists.LinkedList();
			
			var t:int = 1000;
			var v:TestVO;
			var i:int;
			var b:int;
			var end:int;
			var d:int;
			//timer.start();
			b = getTimer();
			for (i = 0; i < t; i++)
			{
				v = new TestVO();
				v.id = i;
				array.push(v);
			}
			end = getTimer();
			d = end-b;
			//timer.stop();
			//Logger.i("Array: " + timer.getSubTime().subTime);
			Logger.i("Array: " + d.toString() );
			
			Logger.i("Creating LinkedList vs Vector", this);
			//timer.start();
			b = getTimer();
			for (i = 0; i < t; i++)
			{
				v = new TestVO();
				v.id = i;	
				linkedList.add(v);
			}
			end = getTimer();
			d = end - b;
			Logger.i("LinkedList: " + d.toString());
			//timer.stop();
			//Logger.i("Collection: " + timer.getSubTime().subTime);
			//timer.start();
			b = getTimer();
			for (i = 0; i < t; i++)
			{
				v = new TestVO();
				v.id = i;	
				vector.push(v);
				//vector[i] = v;
			}
			end = getTimer();
			d = end - b;
			Logger.i("Vector: " + d.toString());
			//timer.stop();
			//Logger.i("Vector: " + timer.getSubTime().subTime);
			
			b = getTimer();
			for (i = 0; i < t; i++)
			{
				v = new TestVO();
				v.id = i;
				dLinked.prepend(v);
			}
			end = getTimer();
			d = end - b;
			Logger.i("DLinked: "+d.toString());
			/*
			b = getTimer();
			for (i = 0; i < t; i++)
			{
				v = new TestVO();
				v.id = i;
				dpdkList.add(v);
			}
			end = getTimer();
			d = end - b;
			Logger.i("DPDK Linked: " + d.toString());
			*/
			Logger.i("---- Iteration times ----");
			
			
			//for each(v in linkedList)
			//var l:LinkedNode = linkedList.iterator();
			
			//Logger.i("size: " + linkedList.size);
			//Logger.i(String(l.next));
			//Logger.i("first: " + linkedList.getFirst());
			//Logger.i("last: " + linkedList.getLast());
			//Logger.i(String(l.element));
			//Logger.i(String(l));
			//Logger.i(String(l.next));
			/*
			var last:LinkedNode = linkedList.header.previous;
			var first:LinkedNode = last.next;
			Logger.i((l == last.next).toString());
			Logger.i("header: "+String(l.element.id));
			Logger.i("last: " + String(last.element.id));
			Logger.i("first: " + String(first.element.id));
			Logger.i("first2: " + linkedList.getFirst().id);
			*/
			var l:LinkedNode = linkedList.header;
			var litr:ListIterator = linkedList.listIteratorAt(0);
			
			timer.start();
			while (litr.hasNext())
			{
				//Logger.i(String(l.element.id));
				//Logger.i(String((l.next == linkedList.header)));
				//Logger.i(n.id.toString());
				//var id:int = l.element.id;
				//Logger.i(l.element.toString());
				litr.next();
				//l = l.next;
				//var id:int = v.id;
			}
			timer.stop();
			Logger.i("Collection time: " + timer.getSubTime().subTime);
			
			timer.start();
			for each(v in vector)
			{
				//Logger.i(n.id.toString());
				//var id2:int = v.id;
			}
			timer.stop();
			Logger.i("Vector time: " + timer.getSubTime().subTime);
			var ld:DListNode = dLinked.head;
			var dLitr:DListIterator = dLinked.getListIterator();
			timer.start();
			while (dLitr.hasNext())
			{
				dLitr.next();
			}
			timer.stop();
			Logger.i("DLinked: " + timer.getSubTime().subTime);
			
			//var dpdkNode:ListI = dpdkList.
			
		}
		
		protected function createList() : void
		{
			/*
			list = new LinkedList();
			list.add(linkedNode);
			*/
		}
		
		/** Private methods **/
		
	}

}